package com.nhimeye.admin.ui;

import java.util.HashSet;

import org.springframework.beans.factory.annotation.Autowired;

import com.nhimeye.admin.app.AbstractEntityView;
import com.nhimeye.admin.app.EntityEditor;
import com.nhimeye.admin.app.EntityProviderUtil;
import com.nhimeye.admin.app.H1;
import com.nhimeye.domain.FieldConfiguration;
import com.nhimeye.domain.FieldValue;
import com.nhimeye.service.FieldValueService;
import com.vaadin.addon.jpacontainer.EntityItem;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.spring.roo.addon.annotations.RooVaadinEntityView;
import com.vaadin.ui.Button;
import com.vaadin.ui.Table;


public class FieldValueView extends AbstractEntityView<FieldValue> {
	@Autowired
	FieldValueService fieldValueService;
	
	private JPAContainer<FieldValue> tableContainer;
	

    @Override
    protected void configureTable(Table table) {
        table.setContainerDataSource(getTableContainer());
        table.setVisibleColumns(getTableColumns());

        
    }

	@Override
	protected HashSet<String> getIgnoreTableColumns() {
		return new HashSet<String>();
	}

	@Override
	protected Class<? extends FieldValue> getEntityClass() {
		 return FieldValue.class;
	}

	@Override
	protected String getEntityName() {
		 return "Field Value";
	}

	@Override
	public boolean isCreateAllowed() {
		return true;
	}

	@Override
	protected boolean isUpdateAllowed() {
		return true;
	}

	@Override
	protected boolean isDeleteAllowed() {
		return true;
	}

	@Override
	protected boolean isNewEntity(FieldValue entity) {
		return (entity != null && getIdForEntity(entity) == null);
    
	}

	@Override
	protected void deleteEntity(FieldValue entity) {
		if (entity != null) {
			fieldValueService.deleteFieldValue(entity);
	        }
	}

	@Override
	protected FieldValue saveEntity(FieldValue entity) {
		if (entity == null) {
            return null;
        }
		fieldValueService.saveFieldValue(entity);
         return entity;
	}

	@Override
	public Object getIdProperty() {
		 return "id";
	}

	@Override
	public Object getVersionProperty() {
		return "version";
	}

	@Override
	protected Container getTableContainer() {
		 if (tableContainer == null) {
	            JPAContainer<FieldValue> container = new JPAContainer<FieldValue>(FieldValue.class);
	            container.setEntityProvider(EntityProviderUtil.get().getEntityProvider(FieldValue.class));
	            tableContainer = container;
	        }
	        return tableContainer;
	}

	@Override
	protected Item getItemForEntity(FieldValue entity) {
		Item item = getTable().getItem(entity.getId());
        if (item == null) {
            item = tableContainer.createEntityItem(entity);
        }
        return item;
	}

	@Override
	protected FieldValue getEntityForItem(Item item) {
		if (item instanceof EntityItem && ((EntityItem) item).getEntity() instanceof FieldValue) {
            return (FieldValue) ((EntityItem) item).getEntity();
        } else {
            return null;
        }
	}

	@Override
	public Object getIdForEntity(FieldValue entity) {
        return entity != null ? entity.getId() : null;
	}

	@Override
	protected FieldValue createEntityInstance() {
		return new FieldValue();
	}

	@Override
	protected void configureButton(Button b) {
		b.setCaption("+ AddFieldValue");
		
	}

	@Override
	protected void configureCaption(H1 title) {
		title.setCaption( "FieldValues");
		
	}

	@Override
	protected EntityEditor createForm() {
		return new FieldValueForm();
	}

}
